'use strict';

angular.module('app')
    .controller('bmBuildingUpdateController', ['$scope', '$rootScope', '$state', '$timeout', 'toaster', '$modal',
        function($scope, $rootScope, $state, $timeout, toaster, $modal) {
            ($state.params.regionName == '广州市') ? $scope.isCityName = true: $scope.isCityName = false;
            $scope.stateRegionId = $state.params.regionId;
            $scope.stateRegionName = $state.params.regionName;
            console.log($state.params.regionId);
            if ($state.includes('**.update')) {
                $scope.title = "修改楼栋信息";
                $scope.reset = false; //修改不允许重置
                console.log($state.params.id)
                var id = $state.params.id;
                //              $scope.record.buildingid= id;
                activate(id);
            } else if ($state.includes('**.create')) {
                $scope.title = "新增楼栋信息";
                $scope.reset = true; //新增时可以重置
                var areaId, areaName;
                if ($state.params.regionName == '广州市') {
                    //选择广州市就不显示区,让他自己选
                    $scope.record = {
                        regionid: $state.params.regionId,
                        areaId: '',
                        areaName: '',
                        status: 1,
                        cityName: "广州市",
                        cityId: "440100",
                        buildingAddress: ''
                    }
                } else {
                    //如果是选择区，就固定区域，不能再改变区
                    //请求区域Code，用于提交
                    $.ajax({
                        type: "put",
                        url: "/dac/bm/building/getAreaId",
                        dataType: 'json',
                        contentType: 'application/json;charset=UTF-8',
                        data: angular.toJson({ areaName: $state.params.regionName })
                    }).then(function(res) {
                        debugger;
                        areaId = res.data.areaId;
                        $scope.record = {
                            regionid: $state.params.regionId,
                            areaId: areaId,
                            areaName: $state.params.regionName,
                            status: 1,
                            cityName: "广州市",
                            cityId: "440100",
                            buildingAddress: ''
                        };
                        $scope.$apply();
                    })

                }

            }

            validate();
            //重置表单
            $scope.resetFrom = function() {
                if ($state.params.regionName == '广州市') {
                    $scope.record = {
                        regionid: $state.params.regionId,
                        areaId: '',
                        areaName: '',
                        status: 1,
                        cityName: "广州市",
                        cityId: "440100",
                        buildingAddress: ''
                    };
                } else {
                    $scope.record = {
                        regionid: $state.params.regionId,
                        areaId: areaId,
                        areaName: $state.params.regionName,
                        status: 1,
                        cityName: "广州市",
                        cityId: "440100",
                        buildingAddress: ''
                    };
                }

                console.log($scope.record);
            }
            $scope.submit = function() {
                var m = $scope.record;
                console.log(m);
                if (m) {
                    $scope.record.buildingAddress = $scope.record.cityName + $scope.record.areaName + $scope.record.streetName + $scope.record.roadName + $scope.record.communityName + $scope.record.buildName;
                    $scope.isDisabled = true;
                    $.ajax({
                        type: 'POST',
                        dataType: 'json',
                        contentType: 'application/json;charset=UTF-8',
                        url: '/dac/bm/building/insertOrUpdate',
                        data: angular.toJson($scope.record)
                    }).then(function(result) {
                        if (result.httpCode == 200) { //成功
                            toaster.clear('*');
                            toaster.pop('success', '', "保存成功");
                            $timeout(function() {
                                $state.go("main.bm.building.list", $state.params);
                            }, 2000);
                        } else {
                            toaster.clear('*');
                            toaster.pop('error', '', result.msg);
                            $scope.isDisabled = false;

                        }
                        $scope.$apply();
                    });
                }
            }
            // 初始化页面
            function activate(id) {
                $scope.loading = true;
                $.ajax({
                    type: 'PUT',
                    url: '/dac/bm/building/detail',
                    dataType: 'json',
                    contentType: 'application/json;charset=UTF-8',
                    data: angular.toJson({ 'id': id })
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                    } else {
                        toaster.clear('*');
                        toaster.pop('error', '', result.msg);
                        $scope.isDisabled = true;
                    }
                    $scope.$apply();

                });
            }

            function validate() {

                jQuery('form').validate({
                    rules: {

                        buildingAddress: {
                            required: true,
                            stringCheck: [],
                            maxLengthB: [50]
                        },
                        buildingNumber: {
                            required: true,
                            stringCheck: [],
                            maxLengthB: [20]
                        }

                    },
                    messages: {

                        buildingAddress: {
                            required: '请填写楼栋地址',
                            maxLengthB: "楼栋地址长度过长"
                        },
                        buildingNumber: {
                            required: '请填写楼栋编号',
                            maxLengthB: "楼栋编号长度过长"
                        }
                    },
                    submitHandler: function() {
                        $scope.submit();
                    }
                });
            }


            //区县模态框
            $scope.showAreaList = function() {
                if ($scope.record.cityId != null) { //上级地址已确定才能选择下级地址，控制地址从上到下选择顺序
                    //每次打开模态框都要清空下级地址
                    //清除街道地址信息
                    $scope.record.streetId = null;
                    $scope.record.streetName = null;
                    $scope.record.streetCode = null;
                    //清除路地址信息
                    $scope.record.roadId = null;
                    $scope.record.roadName = null;
                    $scope.record.roadCode = null;
                    //清除小区地址信息
                    $scope.record.communityId = null;
                    $scope.record.communityName = null;
                    $scope.record.communityCode = null;

                    $scope.items = [];
                    var modalInstance = $modal.open({
                        templateUrl: 'bmAreaList.html', //script标签中定义的id
                        controller: 'bmAreaListCtrl', //modal对应的Controller
                        size: 'lg',
                        resolve: {
                            items: function() {
                                var items = {};
                                items.parent_code = $scope.record.cityId;
                                return items;
                            }
                        }
                    });

                    modalInstance.opened.then(function() { // 模态窗口打开之后执行的函数  

                    });
                    modalInstance.result.then(function(result) {

                        $scope.record.areaId = result[2];
                        $scope.record.areaName = result[1];
                        $scope.record.areaCode = result[0];
                    }, function(reason) {
                        console.log(reason);
                    });
                } else {
                    toaster.clear('*');
                    toaster.pop('error', '', '请先选择地市！');
                }

            };

            //乡镇街道模态框
            $scope.showStreetList = function() {
                console.log($scope.record.areaId)
                if ($scope.record.areaId != null) { //上级地址已确定才能选择下级地址，控制地址从上到下选择顺序
                    //每次打开模态框都要清空下级地址
                    //清除路地址信息
                    $scope.record.roadId = null;
                    $scope.record.roadName = null;
                    $scope.record.roadCode = null;
                    //清除小区地址信息
                    $scope.record.communityId = null;
                    $scope.record.communityName = null;
                    $scope.record.communityCode = null;

                    $scope.items = [];
                    var modalInstance = $modal.open({
                        templateUrl: 'bmStreetList.html', //script标签中定义的id
                        controller: 'bmStreetListCtrl', //modal对应的Controller
                        size: 'lg',
                        resolve: {
                            items: function() {
                                var items = {};
                                items.parent_code = $scope.record.areaId;
                                return items;
                            }
                        }
                    });

                    modalInstance.opened.then(function() { // 模态窗口打开之后执行的函数  

                    });
                    modalInstance.result.then(function(result) {

                        $scope.record.streetId = result[2];
                        $scope.record.streetName = result[1];
                        $scope.record.streetCode = result[0];
                    }, function(reason) {
                        console.log(reason);
                    });
                } else {
                    toaster.clear('*');
                    toaster.pop('error', '', '请先选择区县！');
                }

            };

            //路模态框
            $scope.showRoadList = function() {
                if ($scope.record.streetId != null) { //上级地址已确定才能选择下级地址，控制地址从上到下选择顺序
                    //每次打开模态框都要清空下级地址
                    //清除小区地址信息
                    $scope.record.communityId = null;
                    $scope.record.communityName = null;
                    $scope.record.communityCode = null;

                    $scope.items = [];
                    var modalInstance = $modal.open({
                        templateUrl: 'bmRoadList.html', //script标签中定义的id
                        controller: 'bmRoadListCtrl', //modal对应的Controller
                        size: 'lg',
                        resolve: {
                            items: function() {
                                var items = {};
                                items.parent_code = $scope.record.streetId;
                                return items;
                            }
                        }
                    });

                    modalInstance.opened.then(function() { // 模态窗口打开之后执行的函数  

                    });
                    modalInstance.result.then(function(result) {

                        $scope.record.roadId = result[2];
                        $scope.record.roadName = result[1];
                        $scope.record.roadCode = result[0];
                    }, function(reason) {
                        console.log(reason);
                    });
                } else {
                    toaster.clear('*');
                    toaster.pop('error', '', '请先选择街道！');
                }

            };

            //小区模态框
            $scope.showQuartersList = function() {
                if ($scope.record.roadId != null) { //上级地址已确定才能选择下级地址，控制地址从上到下选择顺序
                    $scope.items = [];
                    var modalInstance = $modal.open({
                        templateUrl: 'bmQuartersList.html', //script标签中定义的id
                        controller: 'bmQuartersListCtrl', //modal对应的Controller
                        size: 'lg',
                        resolve: {
                            items: function() {
                                var items = {};
                                items.parent_code = $scope.record.roadId;
                                return items;
                            }
                        }
                    });

                    modalInstance.opened.then(function() { // 模态窗口打开之后执行的函数  

                    });
                    modalInstance.result.then(function(result) {

                        $scope.record.communityId = result[2];
                        $scope.record.communityName = result[1];
                        $scope.record.communityCode = result[0];
                    }, function(reason) {
                        console.log(reason);
                    });
                } else {
                    toaster.clear('*');
                    toaster.pop('error', '', '请先选择路！');
                }

            };



        }
    ]);

//区县模态框
app.controller('bmAreaListCtrl', function($scope, $rootScope, $modal, $modalInstance, items) {
    $scope.param = {};

    $scope.search = function() {

        $scope.param.type = 'AREAS';
        $scope.param.parent_code = items.parent_code;
        if ($scope.pageFlag != 1) { //未点击翻页
            $scope.param.pageNum = 1; //将页码设置为1，即跳转回第一页
        } else { //点击翻页
            $scope.pageFlag = 2; //将开关值设置为2
        }
        $.ajax({
            type: 'POST',
            dataType: 'json',
            contentType: 'application/json;charset=UTF-8',
            url: '/dic/queryDicList',
            data: angular.toJson($scope.param)
        }).then(function(result) {

            if (result.httpCode == 200) {
                $scope.pageInfo = result;
                console.log(result);
            } else {
                $scope.msg = result.msg;
            }
            $scope.$apply();
        });
    }
    $scope.search();

    $scope.select = function(id_, code_text, code_) {
        $scope.params = [];
        $scope.params[0] = id_;
        $scope.params[1] = code_text;
        $scope.params[2] = code_;
        $modalInstance.close($scope.params);
    };
    $scope.cancel = function() {
        $modalInstance.dismiss('cancel');
    };

    $scope.clear = function() {
        $scope.select('', '');
    }

    // 翻页
    $scope.pagination = function(page) {
        $scope.param.pageNum = page;
        $scope.pageFlag = 1; //将开关值设置为1
        $scope.search();
    };
});

//乡镇街道模态框
app.controller('bmStreetListCtrl', function($scope, $rootScope, $modal, $modalInstance, items) {
    $scope.param = {};

    $scope.search = function() {

        $scope.param.type = 'STREETS';
        $scope.param.parent_code = items.parent_code;
        //          console.log("=======》"+$scope.param.parent_code);
        if ($scope.pageFlag != 1) { //未点击翻页
            $scope.param.pageNum = 1; //将页码设置为1，即跳转回第一页
        } else { //点击翻页
            $scope.pageFlag = 2; //将开关值设置为2
        }
        $.ajax({
            type: 'POST',
            dataType: 'json',
            contentType: 'application/json;charset=UTF-8',
            url: '/dic/queryDicList',
            data: angular.toJson($scope.param)
        }).then(function(result) {

            if (result.httpCode == 200) {
                $scope.pageInfo = result;
                console.log($scope.pageInfo);
            } else {
                $scope.msg = result.msg;
            }
            $scope.$apply();
        });
    }
    $scope.search();

    $scope.select = function(id_, code_text, code_) {
        $scope.params = [];
        $scope.params[0] = id_;
        $scope.params[1] = code_text;
        $scope.params[2] = code_;
        $modalInstance.close($scope.params);
    };
    $scope.cancel = function() {
        $modalInstance.dismiss('cancel');
    };

    $scope.clear = function() {
        $scope.select('', '');
    }

    // 翻页
    $scope.pagination = function(page) {
        $scope.param.pageNum = page;
        $scope.pageFlag = 1; //将开关值设置为1
        $scope.search();
    };
});
//路模态框
app.controller('bmRoadListCtrl', function($scope, $rootScope, $modal, $modalInstance, items) {
    $scope.param = {};

    $scope.search = function() {

        $scope.param.type = 'ROAD';
        $scope.param.parent_code = items.parent_code;
        //          console.log("=======》"+$scope.param.parent_code);
        if ($scope.pageFlag != 1) { //未点击翻页
            $scope.param.pageNum = 1; //将页码设置为1，即跳转回第一页
        } else { //点击翻页
            $scope.pageFlag = 2; //将开关值设置为2
        }
        $.ajax({
            type: 'POST',
            dataType: 'json',
            contentType: 'application/json;charset=UTF-8',
            url: '/dic/queryDicList',
            data: angular.toJson($scope.param)
        }).then(function(result) {

            if (result.httpCode == 200) {
                $scope.pageInfo = result;
                console.log($scope.pageInfo);
            } else {
                $scope.msg = result.msg;
            }
            $scope.$apply();
        });
    }
    $scope.search();

    $scope.select = function(id_, code_text, code_) {
        $scope.params = [];
        $scope.params[0] = id_;
        $scope.params[1] = code_text;
        $scope.params[2] = code_;
        $modalInstance.close($scope.params);
    };
    $scope.cancel = function() {
        $modalInstance.dismiss('cancel');
    };

    $scope.clear = function() {
        $scope.select('', '');
    }

    // 翻页
    $scope.pagination = function(page) {
        $scope.param.pageNum = page;
        $scope.pageFlag = 1; //将开关值设置为1
        $scope.search();
    };
});

//小区模态框
app.controller('bmQuartersListCtrl', function($scope, $rootScope, $modal, $modalInstance, items) {
    $scope.param = {};

    $scope.search = function() {

        $scope.param.type = 'QUARTERS';
        $scope.param.parent_code = items.parent_code;
        //          console.log("=======》"+$scope.param.parent_code);
        if ($scope.pageFlag != 1) { //未点击翻页
            $scope.param.pageNum = 1; //将页码设置为1，即跳转回第一页
        } else { //点击翻页
            $scope.pageFlag = 2; //将开关值设置为2
        }
        $.ajax({
            type: 'POST',
            dataType: 'json',
            contentType: 'application/json;charset=UTF-8',
            url: '/dic/queryDicList',
            data: angular.toJson($scope.param)
        }).then(function(result) {

            if (result.httpCode == 200) {
                $scope.pageInfo = result;
                console.log($scope.pageInfo);
            } else {
                $scope.msg = result.msg;
            }
            $scope.$apply();
        });
    }
    $scope.search();

    $scope.select = function(id_, code_text, code_) {
        $scope.params = [];
        $scope.params[0] = id_;
        $scope.params[1] = code_text;
        $scope.params[2] = code_;
        $modalInstance.close($scope.params);
    };
    $scope.cancel = function() {
        $modalInstance.dismiss('cancel');
    };

    $scope.clear = function() {
        $scope.select('', '');
    }

    // 翻页
    $scope.pagination = function(page) {
        $scope.param.pageNum = page;
        $scope.pageFlag = 1; //将开关值设置为1
        $scope.search();
    };
});